package com.tomjun.permission.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.tomjun.common.JsonModel;
import com.tomjun.permission.mapper.RoleDao;
import com.tomjun.permission.model.Menus;
import com.tomjun.permission.model.RoleInfo;
import com.tomjun.permission.model.UserRolePermissionInfo;
import com.tomjun.permission.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService{
	@Autowired
	private RoleDao roleDao;
	/**
	 * 查看所有角色关联的所有信息，用户以及权限
	 * @param page
	 * @param pageSize
	 * @param map
	 * @return
	 */
	@Override
	public JsonModel<Object> getAllRole(int page, int pageSize, Map<String, Object> map) {
		// TODO Auto-generated method stub
		JsonModel<Object> allRoleModel=new  JsonModel<Object>();
		int count=roleDao.getRoleCount(map);
		PageHelper.startPage(page, pageSize);
		List<UserRolePermissionInfo> list=roleDao.getAllRole(map);
		allRoleModel.setMsg("success");
		allRoleModel.setCode(0);
		allRoleModel.setData(list);
		allRoleModel.setCount(count);
		return allRoleModel;
	}

	/**
	 * 获取所有的模块的权限信息
	 * @return
	 */
	@Override
	public JsonModel<Object> getAllPermission() {
		// TODO Auto-generated method stub
		JsonModel<Object> allPermissionModel=new  JsonModel<Object>();
		List<Menus> list=roleDao.getAllPermission();
		allPermissionModel.setMsg("success");
		allPermissionModel.setCode(0);
		allPermissionModel.setData(list);
		return allPermissionModel;
	}
	/**
	 * 新增角色以及角色权限信息
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void addRole(RoleInfo roleInfo) {
		// TODO Auto-generated method stub
		roleDao.addRole(roleInfo);
		roleDao.addRolePermission(roleInfo.getId(), roleInfo.getPermissionList());
	}
	/**
	 * 更新角色以及角色权限信息
	 * @param roleInfo
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateRole(RoleInfo roleInfo) {
		// TODO Auto-generated method stub
		List<Integer> oldPerms = roleDao.getRoleAllInfo(roleInfo.getId());
		roleDao.updateRole(roleInfo);
		saveNewPermission(roleInfo.getId(),roleInfo.getPermissionList(),oldPerms);
		removeOldPermission(roleInfo.getId(),roleInfo.getPermissionList(),oldPerms);
	}
	
	/**
	 * 为角色添加新权限,这个是用于更新的操作
	 */
	private void saveNewPermission(Integer roleId, List<Integer> newPerms, List<Integer> oldPerms) {
		List<Integer> waitInsert = new ArrayList<>();
		for (Integer newPerm : newPerms) {
			if (!oldPerms.contains(newPerm)) {
				waitInsert.add(newPerm);
			}
		}
		if (waitInsert.size() > 0) {
			roleDao.addRolePermission(roleId, waitInsert);
		}
	}
	
	/**
	 * 删除角色 旧的 不再拥有的权限
	 */
	private void removeOldPermission(Integer roleId, Collection<Integer> newPerms, Collection<Integer> oldPerms) {
		List<Integer> waitRemove = new ArrayList<>();
		for (Integer oldPerm : oldPerms) {
			if (!newPerms.contains(oldPerm)) {
				waitRemove.add(oldPerm);
			}
		}
		if (waitRemove.size() > 0) {
			roleDao.removeOldPermission(roleId, waitRemove);
		}
	}
	/**
	 * 删除角色以及该角色的权限信息
	 * @param roleId
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteRoleById(int roleId) {
		// TODO Auto-generated method stub
		roleDao.deleteRoleById(roleId);
		roleDao.removeAllPermissionByRoleId(roleId);
	}

	@Override
	public boolean existRole(RoleInfo roleInfo) {
		// TODO Auto-generated method stub
		boolean flag=false;
		List<RoleInfo> list=roleDao.existRole(roleInfo.getRoleName());
		for(RoleInfo rf:list) {
			if(rf.getId()!=roleInfo.getId()) {
				flag=true;
				break;
			}
		}
		return flag;
	}

}
